package com.kmmlearn.findtime.android.ui.animation

import androidx.compose.animation.Animatable
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Favorite
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Composable
fun AnimateAsStateDemo1() {
    // 用于控制尺寸的变化
    var change by remember { mutableStateOf(false) }
    // 用于标记点赞状态
    var flag by remember { mutableStateOf(false) }

    val buttonSize by animateDpAsState(
        targetValue = if (change) 40.dp else 20.dp
    )

    val buttonColor by animateColorAsState(
        targetValue = if (flag) Color.Red else Color.Blue
    )

    if (buttonSize == 40.dp) {
        change = false
    }

    IconButton(onClick = {
        change = true
        flag = !flag
    }) {
        Icon(
            Icons.Rounded.Favorite,
            contentDescription = null,
            modifier = Modifier.size(buttonSize),
            tint = buttonColor
        )
    }
}




@Composable
fun AnimateAsStateDemo2() {
    var change by remember { mutableStateOf(false) }
    var flag by remember { mutableStateOf(false) }

    val buttonSize = remember { Animatable(20.dp, Dp.VectorConverter) }
    val buttonColor = remember { Animatable(Color.Gray) }

    // Animatable定义动画animateTo执行需要协程环境
    LaunchedEffect(change, flag) {
        buttonSize.animateTo(if (change) 40.dp else 20.dp)
        buttonColor.animateTo(if (flag) Color.Red else Color.Gray)
    }

    if (buttonSize.value == 40.dp) {
        change = false
    }

    IconButton(onClick = {
        change = true
        flag = !flag
    }) {
        Icon(
            Icons.Rounded.Favorite,
            contentDescription = null,
            modifier = Modifier.size(buttonSize.value),
            tint = buttonColor.value
        )
    }
}
